import {
  Controller,
  Get,
  HttpException,
  HttpStatus,
  Req,
} from '@nestjs/common';
import { UserService } from '../../user.service';
import { Request } from 'express';
import { ApiHeader, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ValidAuthFailDto } from 'src/user/dto/middleware/auth.middleware.dto';
import {
  ProfileOneResFailDto,
  ProfileOneResSuccessDto,
} from 'src/user/dto/profile/one.controller.dto';

@ApiTags('user')
@ApiResponse({
  status: HttpStatus.FORBIDDEN,
  type: ValidAuthFailDto,
  description: '身份验证失败',
})
@Controller('user')
export class GetProfileController {
  constructor(private readonly userService: UserService) {}
  @ApiResponse({
    status: HttpStatus.OK,
    type: ProfileOneResSuccessDto,
    description: '成功获取个人信息',
  })
  @ApiResponse({
    status: HttpStatus.ACCEPTED,
    type: ProfileOneResFailDto,
    description: 'code=20001:用户不存在',
  })
  @ApiHeader({
    name: 'token',
    description: '身份令牌',
  })
  @Get('profile/one')
  async getProfileOne(@Req() req: Request) {
    const openid = req['user'].openid;
    const profile = await this.userService.getProfile(openid);
    if (!profile) {
      throw new HttpException(
        {
          message: '用户不存在',
          code: 20001,
          success: false,
        },
        HttpStatus.ACCEPTED,
      );
    }
    const { property, nickname, tag, allow_stop, head_image } = profile;
    return { success: true, property, nickname, tag, allow_stop, head_image };
  }
}
